package org.rency.mq.kafka.consumer.handler.notify;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.rency.mq.kafka.consumer.handler.AbstractMessageHandler;
import org.rency.mq.kafka.consumer.support.listener.KafkaMessageListener;
import org.springframework.kafka.listener.AbstractMessageListenerContainer;
import org.springframework.kafka.listener.KafkaDataListener;

import java.util.List;

/**
 * 单笔消息处理
 *
 * @author: Created by user_rcy@163.com on 2016/10/21.
 * @version: V1.0.0
 * @since JDK 1.8
 */
public abstract class AbstractNotifyHandler<K, V> extends AbstractMessageHandler<K, V> implements NotifyHandler<K, V>{

    @Override
    public KafkaDataListener getMessageListener() {
        return new KafkaMessageListener<K, V>();
    }

    @Override
    public AbstractMessageListenerContainer.AckMode getAckMode() {
        return AbstractMessageListenerContainer.AckMode.RECORD;
    }

    @Override
    public void handler(List<ConsumerRecord<K, V>> datas) {
        logger.warn("Kafka Consumer Listener Message Handler Error[Batch]!");
        for(ConsumerRecord<K, V> data : datas){
            this.handler(data);
        }
    }

    @Override
    public void handler(ConsumerRecord<K, V> data) {
        try {
            this.handler(data.key(), data.value(), data.topic(), data.partition(), data.timestamp());
        }catch (Exception e){
            logger.error("Kafka Consumer Listener Message Handler Error!",e);
        }
    }

    public abstract void handler(K key,V data,String topic,int partition,long timestamp);

}